package com.csxy.mxj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.mxj.R;
import com.csxy.mxj.entity.BannerQuery;
import com.csxy.mxj.entity.CrmBanner;
import com.csxy.mxj.service.CrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cmsservice/banner")
public class CrmBannerController {
    @Autowired
    private CrmService crmService;
    //多条件分页查询banner(幻灯片)列表
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,
                        @PathVariable long limit,
                        @RequestBody(required = false) BannerQuery bannerQuery) {
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        crmService.pageQuery(bannerPage, bannerQuery);
        //获取数据
        List<CrmBanner> list = bannerPage.getRecords();
        //获取总记录数
        long total = bannerPage.getTotal();
        return R.ok().data("rows",list).data("total",total);
    }

    //添加幻灯片banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        boolean save = crmService.save(crmBanner);
        if (save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //修改幻灯片banner
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean update = crmService.updateById(crmBanner);
        if (update){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //删除幻灯片banner
    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBannerById(@PathVariable String id) {
        boolean delete = crmService.removeById(id);
        if (delete){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据id查询幻灯片banner
    @GetMapping("/getBannerById/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner crmBanner = crmService.getById(id);
        return R.ok().data("item",crmBanner);
    }
}
